//
//  AppDelegate.swift
//  Blockfolio
//
//  Created by Maybe on 08/05/2021.
//

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    var hostReach: Reachability?
    var networkState = 0
    
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        setupGlobalStyle()                       //配置全局样式
        setupRootViewController()                //配置根控制器
        setupReachability()                      //配置网络监听
        //do something
        
        // Override point for customization after application launch.
        return true
    }

    fileprivate func setupGlobalStyle() {
        if #available(iOS 11, *) {
            UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
        }
        UIApplication.shared.isStatusBarHidden = false
        //do something
    }
    
    fileprivate func setupRootViewController(){
        window = UIWindow(frame: Screen_bounds)
        window?.backgroundColor = UIColor.white
        window?.rootViewController = BCBaseTabBarController()
        window?.makeKeyAndVisible()
    }
    
}


// MARK: - 网络状态监测
extension AppDelegate {
    fileprivate func setupReachability(){
        NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(_:)), name: NSNotification.Name.reachabilityChanged, object: nil)
        hostReach = Reachability.forInternetConnection()
        hostReach?.startNotifier()
    }
    
    @objc fileprivate func reachabilityChanged(_ notification: Notification){
        guard let curReach = notification.object as? Reachability  else { return  }
        
        switch curReach.currentReachabilityStatus() {
        case NetworkStatus.NotReachable:
            BClog("无网络")
            networkState = 0
        case NetworkStatus.ReachableViaWWAN:
            BClog("WWan")
            networkState = 1
        case NetworkStatus.ReachableViaWiFi:
            BClog("Wifi")
            networkState = 2
        }
    }
}
